from openpyxl import Workbook


def print_hi(name):
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


if __name__ == '__main__':
    print_hi('text')

# 实例化
wb_write = Workbook()


# 查找文本文件中的指定字符串,若存在，输出所在的函数
# file ———— 文本文件路径
# string  ———— 指定要查找的字符串
def TextFileSearch(file, string):
    count = 0
    # 定义一个list用于保存出现字符串的函数
    line_list = []
    # 下面的encoding可指定编码，如我要查找的文件类型是html文件，所以编码设为utf-8
    f = open(file, 'r', encoding='gb2312')
    for line in f.readlines():
        if string in line:
            print(line[1:24])  # 左闭右开
            line_list.append(count)
        count += 1
    f.close()
    if len(line_list):
        for i in range(len(line_list)):
            print("在" + file + "第" + str(line_list[i]) + " 行已找到.")


TextFileSearch(r"C:\linkmsgd.log", r"bytes to HES")
TextFileSearch(r"C:\linkmsgd.log", r"bytes from HES")

wb_write.save(r"C:\linkmsgd.xlsx")
